Spot-On
spot-on-listener.h
1 /*
2 ** Copyright (c) 2011 - 10^10^10, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from Spot-On without specific prior written permission.
15 **
16 ** SPOT-ON IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** SPOT-ON, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef _spoton_listener_h_
29 #define _spoton_listener_h_
30 
31 #include <QHash>
32 #include <QPointer>
33 #include <QSqlDatabase>
34 #include <QTcpServer>
35 #include <QTimer>
36 #if QT_VERSION >= 0x050501 && defined(SPOTON_BLUETOOTH_ENABLED)
37 #include <qbluetoothserver.h>
38 #include <qbluetoothserviceinfo.h>
39 #include <qbluetoothsocket.h>
40 #endif
41 
42 #include "Common/spot-on-misc.h"
43 #include "spot-on-neighbor.h"
44 
45 class QNetworkInterface;
46 
48 class spoton_sctp_server;
49 
50 class spoton_listener_tcp_server: public QTcpServer
51 {
52  Q_OBJECT
53 
54  public:
55  spoton_listener_tcp_server(const qint64 id,
56  QObject *parent):QTcpServer(parent)
57  {
58  m_id = id;
59  }
60 
62  {
63  }
64 
65  void setMaxPendingConnections(const int maxPendingConnections)
66  {
67  QTcpServer::setMaxPendingConnections(qMax(1, maxPendingConnections));
68  }
69 
70 #if QT_VERSION >= 0x050000
71  void incomingConnection(qintptr socketDescriptor);
72 #else
73  void incomingConnection(int socketDescriptor);
74 #endif
75 
76  private:
77  qint64 m_id;
78 
79  signals:
80 #if QT_VERSION < 0x050000
81  void newConnection(const int socketDescriptor,
82  const QHostAddress &address,
83  const quint16 port);
84 #else
85  void newConnection(const qintptr socketDescriptor,
86  const QHostAddress &address,
87  const quint16 port);
88 #endif
89 };
90 
91 class spoton_listener_udp_server: public QUdpSocket
92 {
93  Q_OBJECT
94 
95  public:
96  spoton_listener_udp_server(const qint64 id,
97  QObject *parent):QUdpSocket(parent)
98  {
99  m_id = id;
100  m_maxPendingConnections = 5;
101  connect(this,
102  SIGNAL(readyRead(void)),
103  this,
104  SLOT(slotReadyRead(void)));
105  }
106 
108  {
109  }
110 
111  bool clientExists(const QHostAddress &address, const quint16 port) const
112  {
113  return m_clients.contains(QString("%1:%2:%3").
114  arg(address.toString()).
115  arg(address.scopeId()).
116  arg(port));
117  }
118 
119  int maxPendingConnections(void) const
120  {
121  return m_maxPendingConnections;
122  }
123 
124  void addClientAddress(const QString &address)
125  {
126  m_clients.insert(address, 0);
127  }
128 
129  void setMaxPendingConnections(const int maxPendingConnections)
130  {
131  m_maxPendingConnections = qMax(1, maxPendingConnections);
132  }
133 
134  private:
135  QHash<QString, char> m_clients;
136  int m_maxPendingConnections;
137  qint64 m_id;
138 
139  private slots:
140  void slotClientDestroyed(QObject *object)
141  {
142  if(!object)
143  return;
144 
145  QString client(object->property("address").toString());
146 
147  m_clients.remove(client);
148  }
149 
150  void slotReadyRead(void);
151 
152  signals:
153 #if QT_VERSION < 0x050000
154  void newConnection(const int socketDescriptor,
155  const QHostAddress &address,
156  const quint16 port);
157 #else
158  void newConnection(const qintptr socketDescriptor,
159  const QHostAddress &address,
160  const quint16 port);
161 #endif
162  void newDatagram(const QByteArray &datagram,
163  const QHostAddress &address,
164  const quint16 port);
165 };
166 
167 class spoton_listener: public QObject
168 {
169  Q_OBJECT
170 
171  public:
172  spoton_listener(const QString &ipAddress,
173  const QString &port,
174  const QString &scopeId,
175  const int maximumClients,
176  const qint64 id,
177  const QString &echoMode,
178  const unsigned int keySize,
179  const QByteArray &certificate,
180  const QByteArray &privateKey,
181  const QByteArray &publicKey,
182  const bool useAccounts,
183  const qint64 maximumBufferSize,
184  const qint64 maximumContentLength,
185  const QString &transport,
186  const bool shareAddress,
187  const QString &orientation,
188  const QString &motd,
189  const QString &sslControlString,
190  const int laneWidth,
191  const int passthrough,
192  const int sourceOfRandomness,
193  const QByteArray &privateApplicationCredentials,
194  const QString &socketOptions,
195  QObject *parent);
196  ~spoton_listener();
197  QHostAddress externalAddress(void) const;
198  QHostAddress localAddress(void) const;
199  QString orientation(void) const;
200  QString serverAddress(void) const;
201  QString transport(void) const;
202  bool isListening(void) const;
203  bool listen(const QString &address, const quint16 port);
204  quint16 externalPort(void) const;
205  quint16 serverPort(void) const;
206  void close(void);
207  void updateConnectionCount(void);
208 
209  private:
210 #if QT_VERSION >= 0x050501 && defined(SPOTON_BLUETOOTH_ENABLED)
211  QBluetoothServer *m_bluetoothServer;
212  QBluetoothServiceInfo m_bluetoothServiceInfo;
213 #endif
214  QByteArray m_certificate;
215  QByteArray m_privateApplicationCredentials;
216  QByteArray m_privateKey;
217  QByteArray m_publicKey;
218  QNetworkInterface *m_networkInterface;
219  QString m_address;
220  QString m_echoMode;
221  QString m_motd;
222  QString m_orientation;
223  QString m_scopeId;
224  QString m_socketOptions;
225  QString m_sslControlString;
226  QString m_transport;
227  QTimer m_externalAddressDiscovererTimer;
228  QTimer m_timer;
229  bool m_shareAddress;
230  bool m_useAccounts;
231  int m_laneWidth;
232  int m_maximumClients;
233  int m_passthrough;
234  int m_sourceOfRandomness;
235  qint64 m_id;
236  qint64 m_maximumBufferSize;
237  qint64 m_maximumContentLength;
238  quint16 m_externalPort;
239  quint16 m_port;
240  spoton_external_address *m_externalAddress;
241  spoton_listener_tcp_server *m_tcpServer;
242  spoton_listener_udp_server *m_udpServer;
243  spoton_sctp_server *m_sctpServer;
244  unsigned int m_keySize;
245  QString errorString(void) const;
246  int maxPendingConnections(void) const;
247  qint64 id(void) const;
248  void prepareNetworkInterface(void);
249  void saveExternalAddress(const QHostAddress &address,
250  const QSqlDatabase &db);
251  void saveStatus(const QSqlDatabase &db);
252 
253  private slots:
254  void slotDiscoverExternalAddress(void);
255  void slotExternalAddressDiscovered(const QHostAddress &address);
256  void slotNeighborDisconnected(void);
257 #if QT_VERSION < 0x050000
258  void slotNewConnection(const int socketDescriptor,
259  const QHostAddress &address,
260  const quint16 port);
261 #else
262  void slotNewConnection(const qintptr socketDescriptor,
263  const QHostAddress &address,
264  const quint16 port);
265 #endif
266 #if QT_VERSION >= 0x050501 && defined(SPOTON_BLUETOOTH_ENABLED)
267  void slotNewConnection(void);
268 #endif
269  void slotTimeout(void);
270 
271  signals:
272  void newNeighbor(QPointer<spoton_neighbor> neighbor);
273 };
274 
275 #endif
Definition: spot-on-sctp-server.h:38
Definition: spot-on-listener.h:91
Definition: spot-on-listener.h:50
Definition: spot-on-listener.h:167
Definition: spot-on-external-address.h:35